﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
using System.IO;
namespace GUI
{
    public partial class uc_DienThoai : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_DienThoai()
        {
            InitializeComponent();
        }
        MobileDTO mobile = new MobileDTO();
        MobileBL mobileBl = new MobileBL();
        private void GetMobileValue()
        {
            if (radioGroup1.SelectedIndex != 0)
            {
                mobile.MobileID = int.Parse(txtMaDT.Text);
            }

            mobile.Name = txtTenDT.Text;
            mobile.Brand = cbbNhanHieu.Text.ToString();
            mobile.Description = txtMota.Text;
            mobile.State = cbbTinhTrang.Text;
            mobile.Prize = int.Parse(txtGiaBan.Text);
            mobile.WarrantyTime = int.Parse(txtBaoHanh.Text);
            mobile.Color = txtMauSac.Text;
            mobile.Count = int.Parse(txtSoLuongTon.Text);
        }
   

        private void btCapNhatImage_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png";
            if (ofd.ShowDialog() == DialogResult.OK)
            {

                txtLinkImage.Text = ofd.FileName.ToString();

                FileStream f = new FileStream(txtLinkImage.Text, FileMode.Open, FileAccess.Read);
                picDienThoai.Image = resizeImage(Image.FromFile(ofd.FileName), picDienThoai.Width, picDienThoai.Height);
            }


        }

        public Image resizeImage(Image img, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage((Image)b);

            g.DrawImage(img, 0, 0, width, height);
            g.Dispose();

            return (Image)b;
        } 

        private void uc_DienThoai_Load(object sender, EventArgs e)
        {
            txtTenDT.Enabled = true;
            cbbNhanHieu.Enabled = true;
            txtSoLuongTon.Enabled = true;
            txtMota.Enabled = true;
            txtMauSac.Enabled = true;
            txtMaDT.Enabled = false;
            txtBaoHanh.Enabled = true;
            cbbTinhTrang.Enabled = true;
            txtGiaBan.Enabled = true;
            mobileTableAdapter.Fill(sellingmobilemanagementDataSet.mobile);
            
        }

        private void txtBaoHanh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;

            }
            Constraint.checkLenght(txtBaoHanh, e, 3);
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                txtMaDT.Enabled = false;
                txtTenDT.Enabled = true;
                cbbNhanHieu.Enabled = true;
                txtSoLuongTon.Enabled = true;
                txtMota.Enabled = true;
                txtMauSac.Enabled = true;
                txtBaoHanh.Enabled = true;
                cbbTinhTrang.Enabled = true;
                txtGiaBan.Enabled = true;
                btnCapNhatImage.Enabled = true;
            }
            else if (radioGroup1.SelectedIndex == 1)
            {
                txtMaDT.Enabled = true;
                txtTenDT.Enabled = true;
                cbbNhanHieu.Enabled = true;
                txtSoLuongTon.Enabled = false;
                txtMota.Enabled = true;
                txtMauSac.Enabled = true;
                txtBaoHanh.Enabled = true;
                cbbTinhTrang.Enabled = false;
                txtGiaBan.Enabled = true;
                btnCapNhatImage.Enabled = true;
            }
            else if (radioGroup1.SelectedIndex == 2)
            {
                txtMaDT.Enabled = true;
                txtTenDT.Enabled = false;
                cbbNhanHieu.Enabled = false;
                txtSoLuongTon.Enabled = false;
                txtMota.Enabled = false;
                txtMauSac.Enabled = false;
                txtBaoHanh.Enabled = false;
                cbbTinhTrang.Enabled = false;
                txtGiaBan.Enabled = false;
                btnCapNhatImage.Enabled = false;
            }
        }

        private void txtGiaBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;

            }
            Constraint.checkLenght(txtGiaBan, e, 8);

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Xác Nhận Thay Đổi?", "Confirmation", MessageBoxButtons.YesNo) ==
              DialogResult.Yes)
            {
                if (radioGroup1.SelectedIndex == 0 && txtLinkImage.Text != "")
                {
                    if (txtTenDT.Text == "" ||
                   txtTenDT.Text == "" ||
                   txtBaoHanh.Text == "" ||
                   txtGiaBan.Text == "" ||
                   txtMauSac.Text == "" ||
                   txtMota.Text == "" ||
                   txtSoLuongTon.Text == "" ||
                   cbbNhanHieu.Text == "" ||
                   cbbTinhTrang.Text == "")
                        MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                    {
                        FileStream f = new FileStream(txtLinkImage.Text, FileMode.Open, FileAccess.Read);
                        byte[] buff = null;
                        BinaryReader br = new BinaryReader(f);
                        buff = br.ReadBytes((int)f.Length);
                        ImageDTO imgdto = new ImageDTO();
                        imgdto.Image = buff;
                        ImageBL imgdal = new ImageBL();
                        imgdal.Image_Insert(imgdto);
                        GetMobileValue();
                        mobile.ImageID = imgdal.Image_SelectByName(buff);
                        mobileBl.Mobile_Insert(mobile);
                        MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtLinkImage.Text = "";
                    }
                   
                }
                else if (radioGroup1.SelectedIndex == 0 && txtLinkImage.Text == "")
                {
                    MessageBox.Show("Chọn ảnh cho điện thoại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else if (radioGroup1.SelectedIndex == 1 && txtLinkImage.Text == "")
                {
                    if (txtMaDT.Text == "")
                        MessageBox.Show("Vui lòng chọn điện thoại cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else if (txtTenDT.Text == "" ||
                       txtTenDT.Text == "" ||
                       txtBaoHanh.Text == "" ||
                       txtGiaBan.Text == "" ||
                       txtMauSac.Text == "" ||
                       txtMota.Text == "" ||
                       txtSoLuongTon.Text == "" ||
                       cbbNhanHieu.Text == "" ||
                       cbbTinhTrang.Text == "")
                        MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                    {
                        mobile.ImageID = Int32.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ImageID").ToString());
                        GetMobileValue();
                        mobileBl.Mobile_Update(mobile);
                        MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else if (radioGroup1.SelectedIndex == 1 && txtLinkImage.Text != "")
                {
                    FileStream f = new FileStream(txtLinkImage.Text, FileMode.Open, FileAccess.Read);
                    byte[] buff = null;
                    BinaryReader br = new BinaryReader(f);
                    buff = br.ReadBytes((int)f.Length);
                    ImageDTO imgdto = new ImageDTO();
                    imgdto.Image = buff;
                    ImageBL imgdal = new ImageBL();
                    imgdal.Image_Insert(imgdto);
                    GetMobileValue();
                    mobile.ImageID = imgdal.Image_SelectByName(buff);
                    mobileBl.Mobile_Update(mobile);
                    MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtLinkImage.Text = "";
                }
                else if (radioGroup1.SelectedIndex == 2)
                {
                    if (txtMaDT.Text == "")
                        MessageBox.Show("Vui lòng chọn điện thoại cần sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                    {
                        mobileBl.Mobile_DeleteByID(mobile);
                        MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                txtMaDT.Text = "";
                txtTenDT.Text = "";
                cbbNhanHieu.Text = "";
                txtSoLuongTon.Text = "";
                txtMota.Text = "";
                txtMauSac.Text = "";
                txtBaoHanh.Text = "";
                cbbTinhTrang.Text = "";
                txtGiaBan.Text = "";
                if (radioGroup1.SelectedIndex == 2)
                { btnCapNhatImage.Enabled = false; }
                else 
                    btnCapNhatImage.Enabled = true;
                mobileTableAdapter.Fill(sellingmobilemanagementDataSet.mobile);
            }
        }

        private void fillToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.mobileTableAdapter.Fill(this.sellingmobilemanagementDataSet.mobile);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }


        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (radioGroup1.SelectedIndex == 2 || radioGroup1.SelectedIndex == 1)
            {
                txtMaDT.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MobileID").ToString();
                txtTenDT.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Name").ToString();
                txtBaoHanh.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "WarrantyTime").ToString();
                txtGiaBan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Prize").ToString();
                txtMauSac.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Color").ToString();
                txtMota.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Description").ToString();
                txtSoLuongTon.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Count").ToString();
                cbbNhanHieu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Brand").ToString();
                cbbTinhTrang.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "State").ToString();
                int imageid = Int32.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ImageID").ToString());
                ImageBL imgbl = new ImageBL();
                ImageDTO imgdto = new ImageDTO();
                imgdto = imgbl.Image_SelectByID(imageid);
                if (imgdto.Image == null)
                    picDienThoai.Image = null;
                else
                {
                    MemoryStream st = new MemoryStream();
                    st.Write(imgdto.Image, 0, imgdto.Image.Length);
                    Image i = Image.FromStream(st);
                    picDienThoai.Image = resizeImage(i, picDienThoai.Width, picDienThoai.Height);
                }
            }
            else
            {
                txtTenDT.Text = "";
                txtTenDT.Text = "";
                txtBaoHanh.Text = "";
                txtGiaBan.Text = "";
                txtMauSac.Text = "";
                txtMota.Text = "";
                txtSoLuongTon.Text = "";
                cbbNhanHieu.Text = "";
                cbbTinhTrang.Text = "";
            }
        }

        private void txtSoLuongTon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            Constraint.checkLenght(txtSoLuongTon, e, 2);
        }

        private void txtMaDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(!char.IsDigit(e.KeyChar)&&(!char.IsControl(e.KeyChar)))
                e.Handled = true;
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                mobile = mobileBl.Mobile_SelectByID(int.Parse(txtMaDT.Text));
                txtTenDT.Text = mobile.Name;
                cbbNhanHieu.Text = mobile.Brand;
                txtSoLuongTon.Text = mobile.Count.ToString();
                txtGiaBan.Text = mobile.Prize.ToString();
                txtMauSac.Text = mobile.Color.ToString();
                txtBaoHanh.Text = mobile.WarrantyTime.ToString();
                cbbTinhTrang.Text = mobile.State.ToString();
                txtMota.Text = mobile.State.ToString();
            }
        }
        

        private void txtTenDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtTenDT, e, 20);
       
            Constraint.checkLenght(txtMota, e, 100);
            
            Constraint.checkLenght(txtMauSac, e, 20);
           

            
            
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            txtTenDT.Text = "";
            txtTenDT.Text = "";
            txtBaoHanh.Text = "";
            txtGiaBan.Text = "";
            txtMauSac.Text = "";
            txtMota.Text = "";
            txtSoLuongTon.Text = "";
            cbbNhanHieu.Text = "";
            cbbTinhTrang.Text = "";
        }


    


    
    }
}
